//
//  EmailDetail.m
//  xjairOA
//
//  Created by apple on 16-1-14.
//  Copyright (c) 2016年 net. All rights reserved.
//

#import "EmailDetail.h"
#import "AppDelegate.h"
#import "Constants.h"

@implementation EmailDetail

AppDelegate * appDelegate;
NSArray * dataArray;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //NSLog(@"%@",self.id);
     self.navigationItem.title = @"邮件详情";
    NSDictionary * params = @{@"model":@"loadOne",@"id":self.id};
    appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate.manager
     POST:emailSubjects
     parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         dataArray = responseObject;
         //[dataArray addObjectsFromArray:responseObject];
        NSDictionary* dict = [dataArray objectAtIndex:0];
         //         self.biaoti.text = [dict objectForKey:@"title"];
         //         self.neirong.text = [dict objectForKey:@"desc"];
         self.subject.text = [dict objectForKey:@"subject"];
         self.sender.text = [dict objectForKey:@"sender"];
         self.sendDate.text = [dict objectForKey:@"sendDate"];
         [self.webView loadHTMLString:[dict objectForKey:@"context"] baseURL:nil];
         //NSLog(@"%@",[dict objectForKey:@"context"]);
//         UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//         [button setTitle:@"test" forState:UIControlStateNormal];
//         button.frame = CGRectMake(10, 5, 50, 50);
//         [button addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
//         [self.buttonView addSubview:button];
         
//         if (![[dict objectForKey:@"attach0"] isEqualToString:@"null"]) {
//             UIButton * button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//             [button1 setTitle:@"附件1" forState:UIControlStateNormal];
//             button1.frame = CGRectMake(0, self.buttonView.bounds.size.height/2-4, 40, 8);
//             [button1 addTarget:self action:@selector(but1Click:) forControlEvents:UIControlEventTouchUpInside];
//             [self.buttonView addSubview:button1];
//         }
         
         int positionX = 0;
         for (int i=0; i<20; i++) {
             NSString * prefix = @"attach";
             NSString * fullname = [prefix stringByAppendingString:[NSString stringWithFormat:@"%d",i]];
             if(![[dict objectForKey:fullname] isEqualToString:@"null"]){
                 UIButton * button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                 [button1 setTitle:[@"附件" stringByAppendingString:[NSString stringWithFormat:@"%d",i+1]] forState:UIControlStateNormal];
                 button1.frame = CGRectMake(positionX, self.buttonView.bounds.size.height/2-4, 40, 8);
                 positionX+=40;
                 [button1 addTarget:self action:@selector(but1Click:) forControlEvents:UIControlEventTouchUpInside];
                 [self.buttonView addSubview:button1];
             }
         }
       
         
//
//         UIButton * button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//          [button1 setTitle:@"附件1" forState:UIControlStateNormal];
//         button1.frame = CGRectMake(0, self.buttonView.bounds.size.height/2-4, 40, 8);
//          [self.buttonView addSubview:button1];
//         
//         UIButton * button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//         [button2 setTitle:@"附件2" forState:UIControlStateNormal];
//          button2.frame = CGRectMake(40, self.buttonView.bounds.size.height/2-4, 40, 8);
//          [self.buttonView addSubview:button2];
//         
//         UIButton * button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//         [button3 setTitle:@"附件3" forState:UIControlStateNormal];
//         button3.frame = CGRectMake(80, self.buttonView.bounds.size.height/2-4, 40, 8);
//          [self.buttonView addSubview:button3];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         //NSLog(@"获取服务器响应出错！");
         UIAlertView * alert = [[UIAlertView alloc]
                                initWithTitle:@"提示"
                                message:@"获取服务器响应出错！"
                                delegate:nil
                                cancelButtonTitle:@"确定"
                                otherButtonTitles:nil];
         [alert show];
     }];

}

- (void) but1Click:(id)sender {
   [self performSegueWithIdentifier:@"emailsegue1" sender:self];
}

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    return NO;//开始不允许跳转，只有当验证账号和密码正确可以进入后由登录代码执行切换
}

@end
